package com.cpi.cornpp.monthsperiods.services;

import java.util.ArrayList;
import java.util.List;

import com.cpi.cornpp.common.exception.CornPPException;
import com.cpi.cornpp.monthsperiods.converter.MonthsPeriodsConverter;
import com.cpi.cornpp.monthsperiods.dao.MonthsPeriodsDAO;
import com.cpi.cornpp.monthsperiods.exception.MonthsPeriodsException;
import com.cpi.cornpp.period.exception.PeriodException;


/**
 * Months Periods Service
 * @author Edgar Joao
 *
 */
public class MonthsPeriodsService {

	
	/**
	 * List Of Purchase Program
	 * 
	 * @author Edgar Joao
	 * @param year
	 * @return
	 */
	public List listOfMonthsPeriods( ) throws MonthsPeriodsException{
		MonthsPeriodsDAO monthsPeriodsDAO = null;
		MonthsPeriodsConverter monthsPeriodsConverter = null;
		List monthsPeriodDTOs = null;
		List monthsPeriodViews = null;
		
		try{			
			monthsPeriodsDAO = new MonthsPeriodsDAO();
			monthsPeriodsConverter = new MonthsPeriodsConverter();
			monthsPeriodDTOs = new ArrayList(0);
			monthsPeriodViews = new ArrayList(0);
			
			monthsPeriodDTOs = monthsPeriodsDAO.listOfMonthsPeriods();
			monthsPeriodViews = monthsPeriodsConverter.convertDTOs(monthsPeriodDTOs);					
			
		} catch (MonthsPeriodsException monthsPeriodsException) {
			monthsPeriodsException.printStackTrace(System.out);
			monthsPeriodsException= new MonthsPeriodsException(monthsPeriodsException, CornPPException.LAYER_SERVICE,CornPPException.ACTION_LISTS);
			throw monthsPeriodsException;
		}catch (Exception e){
			MonthsPeriodsException monthsPeriodsException = null;
			e.printStackTrace(System.out);
			monthsPeriodsException = new MonthsPeriodsException(e , CornPPException.LAYER_SERVICE,CornPPException.ACTION_LISTS);
			throw monthsPeriodsException;
		}
		return monthsPeriodViews;
	}
	/**
	 * Obtiene el periodo actual
	 * @return id de periodo
	 * @throws PeriodException
	 */
	public int actualMonthPeriod()throws MonthsPeriodsException{
		int actualPeriod = 0;
		MonthsPeriodsDAO monthsPeriodsDAO = null;
		try{
			monthsPeriodsDAO = new MonthsPeriodsDAO();
			actualPeriod = monthsPeriodsDAO.actualPeriod();
		}catch(MonthsPeriodsException monthsPeriodsException){
			throw monthsPeriodsException;
		}
		return actualPeriod;
	}
	
	public List listOfMonthsPeriodsForVersion(int versionId, int purchaseOrderVersionId) throws MonthsPeriodsException{
		MonthsPeriodsDAO monthsPeriodsDAO = null;
		MonthsPeriodsConverter monthsPeriodsConverter = null;
		List monthsPeriodForVersionDTOs = null;
		List monthsPeriodViews = null;
		
		try{			
			monthsPeriodsDAO = new MonthsPeriodsDAO();
			monthsPeriodsConverter = new MonthsPeriodsConverter();
			monthsPeriodForVersionDTOs = new ArrayList(0);
			monthsPeriodViews = new ArrayList(0);
			
			monthsPeriodForVersionDTOs = monthsPeriodsDAO.listForVersion(versionId, purchaseOrderVersionId);
			monthsPeriodViews = monthsPeriodsConverter.convertVersionDTOsToViews(monthsPeriodForVersionDTOs);					
			
		} catch (MonthsPeriodsException monthsPeriodsException) {
			monthsPeriodsException.printStackTrace(System.out);
			monthsPeriodsException= new MonthsPeriodsException(monthsPeriodsException, CornPPException.LAYER_SERVICE,CornPPException.ACTION_LISTS);
			throw monthsPeriodsException;
		}catch (Exception e){
			MonthsPeriodsException monthsPeriodsException = null;
			e.printStackTrace(System.out);
			monthsPeriodsException = new MonthsPeriodsException(e , CornPPException.LAYER_SERVICE,CornPPException.ACTION_LISTS);
			throw monthsPeriodsException;
		}
		return monthsPeriodViews;
	}
	
	public List listOfMonthsPeriodsForVersion(int versionId) throws MonthsPeriodsException{
		MonthsPeriodsDAO monthsPeriodsDAO = null;
		MonthsPeriodsConverter monthsPeriodsConverter = null;
		List monthsPeriodForVersionDTOs = null;
		List monthsPeriodViews = null;
		
		try{			
			monthsPeriodsDAO = new MonthsPeriodsDAO();
			monthsPeriodsConverter = new MonthsPeriodsConverter();
			monthsPeriodForVersionDTOs = new ArrayList(0);
			monthsPeriodViews = new ArrayList(0);
			
			monthsPeriodForVersionDTOs = monthsPeriodsDAO.listForVersion(versionId);
			monthsPeriodViews = monthsPeriodsConverter.convertVersionDTOsToViews(monthsPeriodForVersionDTOs);					
			
		} catch (MonthsPeriodsException monthsPeriodsException) {
			monthsPeriodsException.printStackTrace(System.out);
			monthsPeriodsException= new MonthsPeriodsException(monthsPeriodsException, CornPPException.LAYER_SERVICE,CornPPException.ACTION_LISTS);
			throw monthsPeriodsException;
		}catch (Exception e){
			MonthsPeriodsException monthsPeriodsException = null;
			e.printStackTrace(System.out);
			monthsPeriodsException = new MonthsPeriodsException(e , CornPPException.LAYER_SERVICE,CornPPException.ACTION_LISTS);
			throw monthsPeriodsException;
		}
		return monthsPeriodViews;
	}
}
